package com.imooc.miaosha.controller;

import com.imooc.miaosha.config.AccessLimit;
import com.imooc.miaosha.rabbitmq.MQSender;
import com.imooc.miaosha.rabbitmq.MiaoshaMessage;
import com.imooc.miaosha.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.result.Const;
import com.imooc.miaosha.service.MiaoshaService;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Controller
@RequestMapping("/miaosha")
public class MiaoshaController {


    private final MiaoshaService miaoshaService;
    private final MQSender mqSender;

    @Autowired
    public MiaoshaController(MiaoshaService miaoshaService, MQSender mqSender) {
        this.miaoshaService = miaoshaService;
        this.mqSender = mqSender;
    }

    /**
     * QPS:1306
     * 5000 * 10
     */
    @PostMapping("/do_miaosha/{path}")
    @ResponseBody
    @AccessLimit(seconds = 5, maxCount = 5)
    public Result<Integer> miaosha(MiaoshaUser user,
                                   @RequestParam("goodsId") Long goodsId,
                                   @PathVariable("path") String path,
                                   @RequestParam("verifyCode") Integer verifyCode) {
        if (user == null) {
            return Result.error(Const.SESSION_ERROR);
        }
        Result<Integer> result = miaoshaService.getMiaoshaPermission(user, goodsId, path, verifyCode);
        if (result != null) {
            return result;
        }
        //进入mq队列
        MiaoshaMessage miaoshaMessage = new MiaoshaMessage();
        miaoshaMessage.setGoodsId(goodsId);
        miaoshaMessage.setMiaoshaUser(user);
        mqSender.sendMiaoshaMessage(miaoshaMessage);
        return Result.successMsg("排队中");
    }

    @RequestMapping("verifyCode")
    @ResponseBody
    public Result<String> getVerifyCode(MiaoshaUser user,
                                        @RequestParam("goodsId") Long goodsId, HttpServletResponse response) {
        if (user == null) {
            return Result.error(Const.SESSION_ERROR);
        }
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            BufferedImage image = miaoshaService.createVerifyCode(user, goodsId);
            ImageIO.write(image, "jpg", outputStream);
            outputStream.flush();
            outputStream.close();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error(Const.SERVER_ERROR);
        }
    }

    @GetMapping("/get_miaosha_path")
    @ResponseBody
    @AccessLimit(seconds = 5, maxCount = 5)
    public Result<String> getMiaoshaPath(MiaoshaUser user,
                                         @RequestParam("goodsId") Long goodsId) {
        if (user == null) {
            return Result.error(Const.SESSION_ERROR);
        }
        return miaoshaService.getMiaoshaPath(user, goodsId);

    }

    /**
     * 轮询订单是否成功
     *
     * @param user
     * @param goodsId
     * @return 成功  orderId   失败  -1  排队中  0
     */
    @GetMapping("/result")
    @ResponseBody
    public Result<Long> miaoshaResult(MiaoshaUser user,
                                      @RequestParam("goodsId") Long goodsId) {
        if (user == null) {
            return Result.error(Const.SESSION_ERROR);
        }
        Long result = miaoshaService.getMiaoshaResult(user, goodsId);
        return Result.success(result);
    }

}
